<?php

require_once dirname(__FILE__).'/../lib/modelosGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/modelosGeneratorHelper.class.php';

/**
 * modelos actions.
 *
 * @package    gescell
 * @subpackage modelos
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class modelosActions extends autoModelosActions
{
    public function executeNew(sfWebRequest $request)
  {
       $loadstyle = $request->getParameter('loadstylesheet');
       
       $this->form = $this->configuration->getForm();
       $this->modelos = $this->form->getObject();
  }
  public function executeListPrecios(sfWebRequest $request){
    $this->modelos = $this->getRoute()->getObject();
    $this->form = new modelosForm($this->modelos) ;
    $criteria = new Criteria();
    $criteria->add(preciosxmodelosPeer::MODELOS_ID,$this->modelos->getId());
    $this->form->det = new preciosxmodelos();
    $this->form->det = preciosxmodelosPeer::doSelect($criteria);
  }
  public function executeBorrar_detalle(sfWebRequest $request){
      $id = $request->getParameter('id');
      //$criteria = new Criteria;
      //$criteria->add(preciosxmodelosPeer::LISTADEPRECIOS_ID, $precio_id);
      //$criteria->add(preciosxmodelosPeer::MODELOS_ID, $modelo_id);
      $precio = new preciosxmodelos();
      $precio = preciosxmodelosPeer::retrieveByPK($id);
      $modelo_id = $precio->getModelosId();
      $cantidad = $precio->countinformesxingresoss();
      $cantidad2 = $precio->countfallasxingresoss();

      if (!$cantidad && !$cantidad2){
          $precio->delete();
          $criteria = new Criteria();
          $criteria->add(preciosxmodelosPeer::MODELOS_ID,$modelo_id);
          $this->form = new modelosForm();
          $this->form->det = new preciosxmodelos();
          $this->form->det = preciosxmodelosPeer::doSelect($criteria);
          return $this->renderpartial('detalles',array('form' => $this->form));
          
      }
      else{
          $this->getResponse()->setStatusCode(404);
          return $this->renderText('No se Puede Borrar, Existen Registros Asociados');
      }

  }
   public function executeAgregar_con_ajax(){
      $this->forward404Unless($this->getRequest()->isXmlHttpRequest());
      $precios_id = $this->getRequest()->getParameter('listadeprecios_id');
      $modelos_id = $this->getRequest()->getParameter('modelo_id');
      if (!preciosxmodelosPeer::ClaveRepetida($modelos_id, $precios_id)){
              $preciosxmodelos=new preciosxmodelos();
              $preciosxmodelos->setListadepreciosId($precios_id);
              $preciosxmodelos->setmodelosid($modelos_id);
              $preciosxmodelos->setprecio($this->getRequest()->getParameter('precio'));
              $preciosxmodelos->setiva($this->getRequest()->getParameter('iva'));
              $preciosxmodelos->save();
              $criteria = new Criteria();
              $criteria->add(preciosxmodelosPeer::MODELOS_ID,$this->getRequest()->getParameter('modelo_id'));
              $this->form = new preciosxmodelosForm();
              $this->form->det = new preciosxmodelos();
              $this->form->det = preciosxmodelosPeer::doSelect($criteria);
              return $this->renderPartial('detalles', array('form' => $this->form));
      }
      else{
          $this->getResponse()->setStatusCode(404);
          return $this->renderText('Dato Repetido');
      }

  }
  public function executeEditar_datos_ajax(sfWebRequest $request){
         $this->forward404Unless($this->getRequest()->isXmlHttpRequest());
         $precios_id = $this->getRequest()->getParameter('listadeprecios_id');
         $modelos_id = $this->getRequest()->getParameter('modelo_id');
         $c = new criteria;
         $c->add(preciosxmodelosPeer::LISTADEPRECIOS_ID,$precios_id);
         $c->add(preciosxmodelosPeer::MODELOS_ID,$modelos_id);
         $preciosxmodelos=new preciosxmodelos();
         $preciosxmodelos = preciosxmodelosPeer::doSelectOne($c);
         $preciosxmodelos->setprecio($this->getRequest()->getParameter('precio'));
         $preciosxmodelos->setiva($this->getRequest()->getParameter('iva'));
         $preciosxmodelos->save();
              $criteria = new Criteria();
              $criteria->add(preciosxmodelosPeer::MODELOS_ID,$this->getRequest()->getParameter('modelo_id'));
              $this->form = new preciosxmodelosForm();
              $this->form->det = new preciosxmodelos();
              $this->form->det = preciosxmodelosPeer::doSelect($criteria);
              return $this->renderPartial('detalles', array('form' => $this->form));

      //else{
      //    $this->getResponse()->setStatusCode(404);
      //    return $this->renderText('Dato Repetido');

  }
  
}
